package com.my.handler;

import com.my.config.ApiConfig;
import org.jboss.netty.bootstrap.ServerBootstrap;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @Author hujf
 * @Date 2021/10/25 10:58
 * @Description http 命令管理工具
 */
public class CommandManager {

    private static HashMap<String, HttpRpc> map = new HashMap();

    private static CommandManager manager = null;

    public synchronized static CommandManager instance(ServerBootstrap server) {
        if (manager != null) {
            return manager;
        }
        manager = new CommandManager();
        manager.initializedBuiltinRpcs();
        return manager;
    }

    public HttpRpc findHandler(String route) {
        return map.get(route);
    }

    ApiConfig config = new ApiConfig();
    public void initializedBuiltinRpcs() {
        StatusHttp statusHttp = new StatusHttp();
        final Map confMap = this.config.getConfig();
        Iterator<Map.Entry<String,String>> iterator = confMap.entrySet().iterator();
        while(iterator.hasNext()){
            try {
                Map.Entry<String, String> next = iterator.next();
                String key = next.getKey();
                String value = next.getValue();
                final Class<?> aClass = Class.forName(value);
                HttpRpc o = (HttpRpc) aClass.getDeclaredConstructor().newInstance();
                map.put( key,o);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
